package com.luck.pf.framework.logic;

import java.io.Serializable;
import java.sql.Connection;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Criteria;
import org.hibernate.criterion.Restrictions;

import com.luck.pf.common.Constants;
import com.luck.pf.common.entity.UserBean;
import com.luck.pf.framework.dao.HibernateBaseDao;
import com.luck.pf.util.CriteriaUtil;


public class BaseLogic implements IBaseLogic{
	protected HibernateBaseDao baseDao;
	
	public void setBaseDao(HibernateBaseDao baseDao) {
		this.baseDao = baseDao;
	}
	public HibernateBaseDao getBaseDao() {
		return baseDao;
	}

	protected Connection getConnection(){
		return this.baseDao.getSessionFactory().getCurrentSession().connection();
	}
	

	public <T> T load(Class<T> entityClass,Serializable id){
		return baseDao.load(entityClass, id);
	}
	
	public <T> T get(Class<T> entityClass,Serializable id){
		return baseDao.get(entityClass, id);
	}

	public void save(Object obj){
		this.baseDao.save(obj);
	}
	
	public Serializable add(Object obj){
		return this.baseDao.add(obj);
	}
	
	protected void bulkUpdate(String sql,Object... param){
		baseDao.bulkUpdate(sql, param);
	}

	public void delete(Object o) {
		baseDao.delete(o);
	}

	public <T> void deleteById(Class<T> entityClass, Serializable id) {
		baseDao.deleteById(entityClass,id);
	}
	/**
	 * 权限过滤
	 * @param criteria 查询对象
	 * @param orgAlias 过滤机构别名
	 * @param userAlias 过滤用户别名
	 */
	protected void filterData(Criteria criteria,UserBean userInfo,String orgAlias,String userAlias){
		if(userInfo.findResource(Constants.DATA_PRIV_ALL)){
			//查看所有数据	,不做过滤
		}else if(userInfo.findResource(Constants.DATA_PRIV_SELF_AND_BLEW)){
			//本机构数据及本机构以下数据
			CriteriaUtil.addRightLike(criteria, orgAlias+".treeNo", userInfo.getOrg().getTreeNo()+"");
		}else if(userInfo.findResource(Constants.DATA_PRIV_SELF_ORG)){
			//只能看本机构数据
			criteria.add(Restrictions.or(Restrictions.eq(orgAlias+".id", userInfo.getOrgId()), 
					Restrictions.like(orgAlias+".treeNo", userInfo.getOrg().getTreeNo()+"___")));
		}else if(userInfo.findResource(Constants.DATA_PRIV_SELF_DEPT)){
			//只能看本部门数据
			CriteriaUtil.addEq(criteria, orgAlias+".id", userInfo.getDeptId());
		}else if(userInfo.findResource(Constants.DATA_PRIV_SELF)&&StringUtils.isNotBlank(userAlias)){
			//只查看本人数据
			CriteriaUtil.addEq(criteria, userAlias+".id", userInfo.getUserId());
		}else{
			//如果以上都不是,默认查本人
			if(StringUtils.isNotBlank(userAlias)){
				CriteriaUtil.addEq(criteria, userAlias+".id", userInfo.getUserId());
			}else{
				//啥数据也不查，随便给安一个机构ID值
				CriteriaUtil.addEq(criteria, orgAlias+".id", Long.valueOf(-9674));
			}
		}
	}
	

	/**
	 * 权限过滤
	 * @param criteria 查询对象
	 * @param orgAlias 过滤机构别名
	 * @param userAlias 过滤用户别名
	 */
	protected void filterData(StringBuffer sql,Map params,UserBean userInfo,String orgAlias,String userAlias){
		if(userInfo.findResource(Constants.DATA_PRIV_ALL)){
			//查看所有数据	,不做过滤			
		}else if(userInfo.findResource(Constants.DATA_PRIV_SELF_AND_BLEW)){
			//本机构数据及本机构以下数据
			sql.append(" and ");
			sql.append(orgAlias);
			sql.append(".tree_no like :xxxxx_treeNo ");
			params.put("xxxxx_treeNo", userInfo.getOrg().getTreeNo()+"%");
		}else if(userInfo.findResource(Constants.DATA_PRIV_SELF_ORG)){
			//只能看本机构数据
			sql.append(" and ");
			sql.append(" ( " +orgAlias);
			sql.append(".id = :xxxxx_orgId or ");
			sql.append( orgAlias+".tree_no like :xxxxx_treeNo");
			sql.append(" ) ");
			params.put("xxxxx_orgId", userInfo.getOrgId());
			params.put("xxxxx_treeNo", userInfo.getOrg().getTreeNo()+"___");
		}else if(userInfo.findResource(Constants.DATA_PRIV_SELF_DEPT)){
			//只能看本部门数据
			sql.append(" and ");
			sql.append(orgAlias);
			sql.append(".id = :xxxxx_deptId ");
			params.put("xxxxx_deptId", userInfo.getDeptId());
		}else if(userInfo.findResource(Constants.DATA_PRIV_SELF)&&StringUtils.isNotBlank(userAlias)){
			//只查看本人数据
			sql.append(" and ");
			sql.append(userAlias);
			sql.append(".id = :xxxxx_userId ");
			params.put("xxxxx_userId", userInfo.getUserId());
		}else{
			//如果没有设置
			if(StringUtils.isNotBlank(userAlias)){
				//人员别名存在则默认查看本人数据
				sql.append(" and ");
				sql.append(userAlias);
				sql.append(".id = :xxxxx_userId ");
				params.put("xxxxx_userId", userInfo.getUserId());
			}else{
				//否则随便查一个不存在的机构ID
				sql.append(" and ");
				sql.append(orgAlias);
				sql.append(".id = :xxxxx_deptId ");
				params.put("xxxxx_deptId", Long.valueOf(-8375));
			}
		}
	}
}
